﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using acessaDB;
using Frame;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using Estrutura;


namespace Banco
{

    /// <summary>
    /// Summary description for BD_Sala
    /// </summary>
    public class BD_Sala
    {
        #region variaveis_globais

        acessaDB.conectaDBMSSQLServer sqlServer = new conectaDBMSSQLServer();
        string conectionString = System.Configuration.ConfigurationManager.AppSettings["SQLServerConnectionLocal"];
        string usuario = string.Empty;

        #endregion

        #region construtor

        public BD_Sala()
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["SQLServerConnectionProd"];
        }
        public BD_Sala(string usu_Login)
        {
            if (System.Configuration.ConfigurationManager.AppSettings["ambiente"] == "PROD")
                conectionString = System.Configuration.ConfigurationManager.AppSettings["SQLServerConnectionProd"];
            usuario = usu_Login;
        }

        #endregion

        #region metodos

        public System.Data.DataSet _retorna_Sala(
                                                string SALA_ID,
                                                string SAL_DESCRICAO,
                                                string SAL_EXCLUIDO
                                                )
        {

            if (string.IsNullOrEmpty(SALA_ID)) SALA_ID = "''";
            if (string.IsNullOrEmpty(SAL_DESCRICAO)) SAL_DESCRICAO = "''";
            if (string.IsNullOrEmpty(SAL_EXCLUIDO)) SAL_EXCLUIDO = "''";

            string sql = "SELECT " +
                                  "SALA.SALA_ID, " +
                                  "SALA.SAL_DESCRICAO, " +
                                  "SALA.SAL_EXCLUIDO " +
                            "FROM SALA " +
                            "WHERE " +
                            "      (SALA.SALA_ID = " + SALA_ID + "  OR " + SALA_ID + " = '') AND " +
                            "      (SALA.SAL_DESCRICAO = " + SAL_DESCRICAO + "  OR " + SAL_DESCRICAO + " = '') AND " +
                            "      (SALA.SAL_EXCLUIDO = " + SAL_EXCLUIDO + "  OR " + SAL_EXCLUIDO + " = '') ";

            return sqlServer.retornaDataSet(sql, conectionString);;
            

        }


        public List<ES_Sala> _obter_Sala(
                                                string SALA_ID,
                                                string SAL_DESCRICAO,
                                                string SAL_EXCLUIDO
                                                )
        {

            if (string.IsNullOrEmpty(SALA_ID)) SALA_ID = "0";
            if (string.IsNullOrEmpty(SAL_DESCRICAO)) SAL_DESCRICAO = "''"; else SAL_DESCRICAO = Util.retonarAspas(SAL_DESCRICAO + "%");
            if (string.IsNullOrEmpty(SAL_EXCLUIDO)) SAL_EXCLUIDO = "-1";

            string sql = "SELECT " +
                                  "SALA.SALA_ID, " +
                                  "SALA.SAL_DESCRICAO, " +
                                  "SALA.SAL_EXCLUIDO " +
                            "FROM SALA " +
                            "WHERE " +
                            "      (SALA.SALA_ID = " + SALA_ID + "  OR " + SALA_ID + " = 0) AND " +
                            "      (SALA.SAL_DESCRICAO LIKE " + SAL_DESCRICAO + "  OR " + SAL_DESCRICAO + " = '') AND " +
                            "      (SALA.SAL_EXCLUIDO = " + SAL_EXCLUIDO + "  OR " + SAL_EXCLUIDO + " = -1) ";

            List<ES_Sala> lista = new List<ES_Sala>();
            SqlConnection sqlConexao = new SqlConnection(conectionString);
            SqlDataReader dr = sqlServer.retornaDataReader(sql, conectionString, sqlConexao);
            try
            {
                while (dr.Read())
                {
                    ES_Sala item = new ES_Sala();
                    item.SALA_ID = dr["SALA_ID"].ToString();
                    item.SAL_DESCRICAO = dr["SAL_DESCRICAO"].ToString();                    
                    item.SAL_EXCLUIDO = (dr["SAL_EXCLUIDO"].ToString() == "0") ? false : true;
                    lista.Add(item);

                }
                return lista;
            }
            finally
            {
                sqlServer.fecharConexao(sqlConexao);
            }
        }


        public ES_Sala _obter_Sala_by_id(
                                                string SALA_ID
                                                )
        {

            if (string.IsNullOrEmpty(SALA_ID)) SALA_ID = "'0'";

            string sql = "SELECT " +
                                  "SALA.SALA_ID, " +
                                  "SALA.SAL_DESCRICAO, " +
                                  "SALA.SAL_EXCLUIDO " +
                            "FROM SALA " +
                            "WHERE " +
                            "      (SALA.SALA_ID = " + SALA_ID + "  OR " + SALA_ID + " = '0') ";

            ES_Sala item = new ES_Sala();
            SqlConnection sqlConexao = new SqlConnection(conectionString);
            SqlDataReader dr = sqlServer.retornaDataReader(sql, conectionString, sqlConexao);
            try
            {
                while (dr.Read())
                {
                    item.SALA_ID = dr["SALA_ID"].ToString();
                    item.SAL_DESCRICAO = dr["SAL_DESCRICAO"].ToString();
                    item.SAL_EXCLUIDO = (dr["SAL_EXCLUIDO"].ToString() == "0") ? false : true;

                }
                return item;
            }
            finally
            {
                sqlServer.fecharConexao(sqlConexao);
            }
        }


        public void _inserir_Sala(
                                                string SALA_ID,
                                                string SAL_DESCRICAO,
                                                bool SAL_EXCLUIDO
                                                )
        {

            string sql = " INSERT INTO SALA " +
                         "      ( " +
                         "          SAL_DESCRICAO, " +
                         "          SAL_EXCLUIDO  " +
                 "      )" +
                 "  VALUES " +
                 "      ( " +
                              Util.retonarAspas(SAL_DESCRICAO, true) + ", " +
                              ((SAL_EXCLUIDO == true) ? "1" : "0") +
                 "      )";
            try
            {
                sqlServer._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _atualizar_Sala(
                                                string SALA_ID,
                                                string SAL_DESCRICAO,
                                                bool SAL_EXCLUIDO
                                                )
        {

            string sql = " UPDATE SALA SET " +
                         "      SAL_DESCRICAO = " + Util.retonarAspas(SAL_DESCRICAO, true) + ", " +
                         "      SAL_EXCLUIDO = " + ((SAL_EXCLUIDO == true) ? "1" : "0") +
                         " WHERE SALA_ID =  " + SALA_ID;
            try
            {
                sqlServer._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        public void _excluir_Sala(string SALA_ID)
        {
            string sql = "DELETE FROM SALA WHERE SALA_ID = " + SALA_ID;
            try
            {
                sqlServer._ExecutarComandoRetornandoID(sql, conectionString, false, usuario);
            }
            catch
            { }
        }



        #endregion
    }
}
